/**
 * @author Amamatthew
 * @class DragonApp.view.pub.form.FileUpload
 * @extends extend: 'Ext.form.Panel',
 *
 *  实际用例:
 *
 */
Ext.define('DragonApp.view.notice.FileUpload', {
    extend: 'Ext.form.Panel',
    alias: 'widget.fileuploadform',
    config: {
        fieldLabel: undefined,
        destUrl: undefined
    },
    bodyPadding: 10,
    frame: true,
    items: [{
        xtype: 'filefield',
        name: 'file',
        itemId: 'filefield',
        fieldLabel: 'Photo',
        labelWidth: 70,
        msgTarget: 'side',
        allowBlank: false,
        anchor: '100%',
        buttonText: '选择文件...'
    }, {
        xtype: 'container',
        items: [{
            xtype: 'image',
            alt: '图像预览',
            src: '',
            width: 184,
            height: 90,
            margin: '10 0 0 0',
            itemId: 'previewfield'
        }]
    }, {
        xtype: 'displayfield',
        name: 'filename'
    }],
    buttons: [{
        text: '上传',
        itemId: 'uploadBtn',
        url: undefined,
        handler: function(button, e) {
            var form = this.up('form');
            var basicform = this.up('form').getForm();
            var formValue = basicform.getValues();

            if (basicform.isValid()) {

                //上传文件提交
                basicform.submit({
                    url: this.up('form').destUrl,
                    waitMsg: '上传中...',
                    success: function(form, action) {

                        //成功提示
                        DragonApp.util.Msg.showToast(action.result.msg);

                        var data = action.result.data;

                        //更新预览
                        var filename = data.filename;
                        var container = Ext.ComponentQuery.query('fileuploadform>container')[0];
                        var previewfield = container.queryById('previewfield');

                        previewfield.setSrc(DragonApp.ux.config.Config.getNoticeUrl(filename));

                        //文件名称
                        var nameField = Ext.ComponentQuery.query('displayfield[name="filename"]')[0];
                        nameField.setValue("@filename："+data.filename);

                        //删除同名记录,并增加新记录
                        var store = Ext.data.StoreManager.lookup('selectedStore');
                        store.remove(store.queryRecords('filename', data.filename));

                        store.add(Ext.create('DragonApp.model.Image', {
                            ext: data.ext,
                            tag: data.tag,
                            path: data.path,
                            width: data.width,
                            height: data.height,
                            filename: data.filename,
                            filesize: data.filesize,
                            lastmodtime: data.lastmodtime
                        }));
                        return;
                    },
                    failure: function(form, action) {

                        switch (action.failureType) {
                            case Ext.form.action.Action.CLIENT_INVALID:
                                Ext.Msg.alert('提示', 'Form fields may not be submitted with invalid values');
                                break;
                            case Ext.form.action.Action.CONNECT_FAILURE:
                                Ext.Msg.alert('提示', 'Ajax communication failed');
                                break;
                            case Ext.form.action.Action.SERVER_INVALID:
                                Ext.Msg.alert('提示', action.result.msg);
                        }
                    }
                });
            }
        }
    }],
    initComponent: function() {
        me = this;
        me.callParent(arguments);

        var filefield = me.queryById('filefield');
        filefield.setFieldLabel(this.fieldLabel);
    }
});